<?php

namespace app\models;

use yii;
use app\components\BaseActiveRecord;

/**
 * This is the model class for table "{{%comment}}".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property integer $uid
 * @property integer $own
 * @property integer $own_id
 * @property string $content
 * @property integer $likes
 * @property integer $status
 * @property integer $ctime
 *
 * @property User $user
 * @property Like $like
 * @property $children
 */
class Comment extends BaseActiveRecord {

    const OWN_ACTIVITY = Activity::TYPE_ACTIVITY;
    const OWN_JOB = Activity::TYPE_JOB;
    const OWN_POST = 3;

    public static $ownLabels = [
        self::OWN_ACTIVITY => '活动',
        self::OWN_JOB => '实习兼职',
        self::OWN_POST => '文章',
    ];

    public static $statusLabels = [
        self::STATUS_YES => '显示',
        self::STATUS_NO => '已屏蔽',
    ];

    public static $statusColors = [
        self::STATUS_NO => 'warning',
        self::STATUS_YES => 'info',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%comment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            ['own', 'in', 'range' => [static::OWN_ACTIVITY, static::OWN_JOB, static::OWN_POST]],
            [['uid', 'parent_id', 'own', 'own_id', 'likes', 'status', 'ctime'], 'integer'],
            [['content', 'uid', 'own', 'own_id'], 'required'],
            [['content'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'own' => 'Own',
            'own_id' => 'Own ID',
            'content' => '内容',
            'likes' => 'Likes',
            'status' => '状态',
            'ctime' => '评论时间',
        ];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function getChildren() {
        return $this->hasMany(Comment::className(), ['parent_id' => 'id']);
    }

    public function getLike() {
        return $this->hasOne(Like::className(), ['own_id' => 'id']);
    }

    public function getIsDeny() {
        return intval($this->status) === intval(static::STATUS_NO);
    }

    public function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);

        if (!$insert) {
            return;
        }

        $modelClasses = [
            static::OWN_ACTIVITY => Activity::className(),
            static::OWN_JOB => Activity::className(),
            static::OWN_POST => Post::className(),
        ];
        if (!isset($modelClasses[$this->own])) {
            return;
        }

        $modelClass = $modelClasses[$this->own];

        /* @var $model \app\components\BaseActiveRecord */
        $model = $modelClass::findOne($this->own_id);
        if ($model) {
            $value = $model->getAttribute('comments') + 1;
            $model->setAttribute('comments', $value);
            $model->save(FALSE);
        }
    }
}
